﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Activity.Data;
using Activity.Helper;
using Activity.Model;
using com.km.Cache;

namespace Activity.API
{
    public class KMSalesYearOnYearReportHandler
    {
        public IList<KMSalesYearOnYearReportModel.SqlModel> GetAllReport(DateTime EndDate1,
            DateTime EndDate2, DateTime EndDate3)
        {
            try
            {
                var time1 = $"{EndDate1:yyyy-MM-dd}";
                var time2 = $"{EndDate2:yyyy-MM-dd}";
                var time3 = $"{EndDate3:yyyy-MM-dd}";
                var sql =
                    $@"exec dbo.KM_Sales_YearOnYear 'KM','{time1}','{EndDate1:yyyy-MM-dd HH:00:00}','{time2}','{
                            EndDate2
                        :yyyy-MM-dd HH:00:00}','{time3}','{EndDate3:yyyy-MM-dd HH:00:00}'";
                var db = new HKERPEntities();
                var query = db.Database.SqlQuery<KMSalesYearOnYearReportModel.SqlModel>(sql).ToList();
                var json = query.ToJson();
                var bt = Encoding.UTF8.GetBytes(json);
                var key = $@"{EndDate1}_{EndDate2}_{EndDate3}";
                RedisCache.Add(key, bt, 20);
                return query;
            }
            catch (Exception ex)
            {
                throw(ex);
            }
        }

        public IList<KMSalesYearOnYearReportModel.ShowModel> GetOperatorAreaReport(DateTime EndDate1,
            DateTime EndDate2, DateTime EndDate3)
        {
            try
            {
                var time1 = $"{EndDate1:yyyy-MM-dd}";
                var time2 = $"{EndDate2:yyyy-MM-dd}";
                var time3 = $"{EndDate3:yyyy-MM-dd}";
                var key = $@"{EndDate1}_{EndDate2}_{EndDate3}";
                var query = new List<KMSalesYearOnYearReportModel.SqlModel>();
                if (RedisCache.Exists(key))
                {
                    byte[] bt = RedisCache.Get(key);
                    var json = Encoding.UTF8.GetString(bt);
                    query = json.FromJson<IList<KMSalesYearOnYearReportModel.SqlModel>>().ToList();
                }
                else
                {
                    query = GetAllReport(EndDate1, EndDate2, EndDate3).ToList();
                }
                var group = query.Where(d => d.Level == 0)
                    .Select(d => new KMSalesYearOnYearReportModel.OperatorAreaModel
                    {
                        CompanyID = d.CompanyID,
                        OperationAreaID = d.OperationAreaID,
                        OperationAreaName = d.OperationAreaName,
                        Level = d.Level,
                        SaleDate = d.SaleDate,
                        SaleAmount = d.SaleAmount,
                        CalSaleAmount12 = d.CalSaleAmount12,
                        CalSaleAmount2 = d.CalSaleAmount2,
                        CalSaleAmount13 = d.CalSaleAmount13,
                        CalSaleAmount3 = d.CalSaleAmount3,
                    }).GroupBy(d => new {d.OperationAreaID, d.OperationAreaName});
                var list = group.SelectMany(d => d).Select(d => new KMSalesYearOnYearReportModel.OperatorAreaModel
                {
                    CompanyID = d.CompanyID,
                    OperationAreaID = d.OperationAreaID,
                    OperationAreaName = d.OperationAreaName,
                    SaleDate = d.SaleDate,
                    Level = d.Level,
                    SaleAmount = d.SaleAmount,
                    CalSaleAmount12 = d.CalSaleAmount12,
                    CalSaleAmount2 = d.CalSaleAmount2,
                    CalSaleAmount13 = d.CalSaleAmount13,
                    CalSaleAmount3 = d.CalSaleAmount3,
                }).ToList();
                foreach (var item in group)
                {
                    var date1 = item.FirstOrDefault(d => d.SaleDate == time1);
                    var date2 = item.FirstOrDefault(d => d.SaleDate == time2);
                    var date3 = item.FirstOrDefault(d => d.SaleDate == time3);
                    if (date1 == null)
                    {
                        var newItem = new KMSalesYearOnYearReportModel.OperatorAreaModel
                        {
                            OperationAreaID = item.Key.OperationAreaID,
                            OperationAreaName = item.Key.OperationAreaName,
                            SaleDate = time1,
                            SaleAmount = 0,
                            Level = 0,
                            CalSaleAmount12 = 0,
                            CalSaleAmount2 = 0,
                            CalSaleAmount13 = 0,
                            CalSaleAmount3 = 0,
                        };
                        list.Add(newItem);
                    }
                    if (date2 == null)
                    {
                        var newItem = new KMSalesYearOnYearReportModel.OperatorAreaModel
                        {
                            OperationAreaID = item.Key.OperationAreaID,
                            OperationAreaName = item.Key.OperationAreaName,
                            SaleDate = time2,
                            SaleAmount = 0,
                            Level = 0,
                            CalSaleAmount12 = 0,
                            CalSaleAmount2 = 0,
                            CalSaleAmount13 = 0,
                            CalSaleAmount3 = 0,
                        };
                        list.Add(newItem);
                    }
                    if (date3 == null)
                    {
                        var newItem = new KMSalesYearOnYearReportModel.OperatorAreaModel
                        {
                            OperationAreaID = item.Key.OperationAreaID,
                            OperationAreaName = item.Key.OperationAreaName,
                            SaleDate = time3,
                            SaleAmount = 0,
                            Level = 0,
                            CalSaleAmount12 = 0,
                            CalSaleAmount2 = 0,
                            CalSaleAmount13 = 0,
                            CalSaleAmount3 = 0,
                        };
                        list.Add(newItem);
                    }
                }
                var totalDate1 = list.Where(d => d.SaleDate == time1);
                var totalItem1 = new KMSalesYearOnYearReportModel.OperatorAreaModel
                {
                    OperationAreaName = "总计",
                    OperationAreaID = "KM9999999999999999999",
                    SaleDate = time1,
                    SaleAmount = totalDate1.Sum(d => d.SaleAmount),
                    Level = 0,
                    CalSaleAmount12 = totalDate1.Sum(d => d.CalSaleAmount12),
                    CalSaleAmount13 = totalDate1.Sum(d => d.CalSaleAmount13),
                    CalSaleAmount2 = totalDate1.Sum(d => d.CalSaleAmount2),
                    CalSaleAmount3 = totalDate1.Sum(d => d.CalSaleAmount3),
                };
                var totalDate2 = list.Where(d => d.SaleDate == time2);
                var totalItem2 = new KMSalesYearOnYearReportModel.OperatorAreaModel
                {
                    OperationAreaName = "总计",
                    OperationAreaID = "KM9999999999999999999",
                    SaleDate = time2,
                    SaleAmount = totalDate2.Sum(d => d.SaleAmount),
                    Level = 0,
                    CalSaleAmount12 = totalDate2.Sum(d => d.CalSaleAmount12),
                    CalSaleAmount13 = totalDate2.Sum(d => d.CalSaleAmount13),
                    CalSaleAmount2 = totalDate2.Sum(d => d.CalSaleAmount2),
                    CalSaleAmount3 = totalDate2.Sum(d => d.CalSaleAmount3),
                };
                var totalDate3 = list.Where(d => d.SaleDate == time3);
                var totalItem3 = new KMSalesYearOnYearReportModel.OperatorAreaModel
                {
                    OperationAreaName = "总计",
                    OperationAreaID = "KM9999999999999999999",
                    SaleDate = time3,
                    SaleAmount = totalDate3.Sum(d => d.SaleAmount),
                    Level = 0,
                    CalSaleAmount12 = totalDate3.Sum(d => d.CalSaleAmount12),
                    CalSaleAmount13 = totalDate3.Sum(d => d.CalSaleAmount13),
                    CalSaleAmount2 = totalDate3.Sum(d => d.CalSaleAmount2),
                    CalSaleAmount3 = totalDate3.Sum(d => d.CalSaleAmount3),
                };
                list.Add(totalItem1);
                list.Add(totalItem2);
                list.Add(totalItem3);
                var newList = new List<KMSalesYearOnYearReportModel.ShowModel>();
                var newGroup = list.OrderBy(d => d.OperationAreaID)
                    .GroupBy(d => new {d.OperationAreaID, d.OperationAreaName});
                foreach (var item in newGroup)
                {
                    var data1 = item.First(d => d.SaleDate == time1);
                    var data2 = item.First(d => d.SaleDate == time2);
                    var data3 = item.First(d => d.SaleDate == time3);
                    var newItem = new KMSalesYearOnYearReportModel.ShowModel
                    {
                        OperationAreaOrShopID = data1.OperationAreaID,
                        OperationAreaOrShopName = data1.OperationAreaName,
                        Level = data1.Level,
                        SaleDate1 = time1,
                        SaleAmount1 = data1.SaleAmount,
                        SaleDate2 = time2,
                        SaleAmount2 = data2.SaleAmount,
                        SaleDate3 = time3,
                        SaleAmount3 = data3.SaleAmount,
                    };
                    newItem.Compared2 = data2.CalSaleAmount2 == 0
                        ? 0
                        : ((data1.CalSaleAmount12 / data2.CalSaleAmount2) - 1) * 100;
                    newItem.Compared3 = data3.CalSaleAmount3 == 0
                        ? 0
                        : ((data1.CalSaleAmount13 / data3.CalSaleAmount3) - 1) * 100;
                    newList.Add(newItem);
                }
                return newList;
            }
            catch (Exception ex)
            {
                throw(ex);
            }
        }

        public IList<KMSalesYearOnYearReportModel.ShowModel> GetShopReport(DateTime EndDate1,
            DateTime EndDate2, DateTime EndDate3)
        {
            try
            {
                var time1 = $"{EndDate1:yyyy-MM-dd}";
                var time2 = $"{EndDate2:yyyy-MM-dd}";
                var time3 = $"{EndDate3:yyyy-MM-dd}";
                var key = $@"{EndDate1}_{EndDate2}_{EndDate3}";
                var query = new List<KMSalesYearOnYearReportModel.SqlModel>();
                if (RedisCache.Exists(key))
                {
                    byte[] bt = RedisCache.Get(key);
                    var json = Encoding.UTF8.GetString(bt);
                    query = json.FromJson<IList<KMSalesYearOnYearReportModel.SqlModel>>().ToList();
                }
                else
                {
                    query = GetAllReport(EndDate1, EndDate2, EndDate3).ToList();
                }
                var group = query.Where(d => d.Level == 1)
                    .Select(d => new KMSalesYearOnYearReportModel.ShopModel
                    {
                        CompanyID = d.CompanyID,
                        ShopID = d.ShopID,
                        ShopName = d.ShopName,
                        Level = d.Level,
                        SaleDate = d.SaleDate,
                        SaleAmount = d.SaleAmount,
                    }).GroupBy(d => new {d.ShopID, d.ShopName});
                var list = group.SelectMany(d => d).Select(d => new KMSalesYearOnYearReportModel.ShopModel
                {
                    CompanyID = d.CompanyID,
                    ShopID = d.ShopID,
                    ShopName = d.ShopName,
                    Level = d.Level,
                    SaleAmount = d.SaleAmount,
                    SaleDate = d.SaleDate,
                }).ToList();
                foreach (var item in group)
                {
                    var date1 = item.FirstOrDefault(d => d.SaleDate == time1);
                    var date2 = item.FirstOrDefault(d => d.SaleDate == time2);
                    var date3 = item.FirstOrDefault(d => d.SaleDate == time3);
                    if (date1 == null)
                    {
                        var newItem = new KMSalesYearOnYearReportModel.ShopModel
                        {
                            ShopID = item.Key.ShopID,
                            ShopName = item.Key.ShopName,
                            SaleDate = time1,
                            SaleAmount = 0,
                            Level = 1,
                        };
                        list.Add(newItem);
                    }
                    if (date2 == null)
                    {
                        var newItem = new KMSalesYearOnYearReportModel.ShopModel
                        {
                            ShopID = item.Key.ShopID,
                            ShopName = item.Key.ShopName,
                            SaleDate = time2,
                            SaleAmount = 0,
                            Level = 1,
                        };
                        list.Add(newItem);
                    }
                    if (date3 == null)
                    {
                        var newItem = new KMSalesYearOnYearReportModel.ShopModel
                        {
                            ShopID = item.Key.ShopID,
                            ShopName = item.Key.ShopName,
                            SaleDate = time3,
                            SaleAmount = 0,
                            Level = 1,
                        };
                        list.Add(newItem);
                    }
                }
                var totalDate1 = list.Where(d => d.SaleDate == time1);
                var totalItem1 = new KMSalesYearOnYearReportModel.ShopModel
                {
                    ShopName = "总计",
                    ShopID = "KM9999999999999999999",
                    SaleDate = time1,
                    SaleAmount = totalDate1.Sum(d => d.SaleAmount),
                    Level = 1,
                };
                var totalDate2 = list.Where(d => d.SaleDate == time2);
                var totalItem2 = new KMSalesYearOnYearReportModel.ShopModel
                {
                    ShopName = "总计",
                    ShopID = "KM9999999999999999999",
                    SaleDate = time2,
                    SaleAmount = totalDate2.Sum(d => d.SaleAmount),
                    Level = 1,
                };
                var totalDate3 = list.Where(d => d.SaleDate == time3);
                var totalItem3 = new KMSalesYearOnYearReportModel.ShopModel
                {
                    ShopName = "总计",
                    ShopID = "KM9999999999999999999",
                    SaleDate = time3,
                    SaleAmount = totalDate3.Sum(d => d.SaleAmount),
                    Level = 1,
                };
                list.Add(totalItem1);
                list.Add(totalItem2);
                list.Add(totalItem3);
                var newList = new List<KMSalesYearOnYearReportModel.ShowModel>();
                var newGroup = list.OrderBy(d => d.ShopID).GroupBy(d => new {d.ShopID, d.ShopName});
                foreach (var item in newGroup)
                {
                    var data1 = item.FirstOrDefault(d => d.SaleDate == time1);
                    var data2 = item.FirstOrDefault(d => d.SaleDate == time2);
                    var data3 = item.FirstOrDefault(d => d.SaleDate == time3);
                    var newItem = new KMSalesYearOnYearReportModel.ShowModel
                    {
                        OperationAreaOrShopID = data1.ShopID,
                        OperationAreaOrShopName = data1.ShopName,
                        Level = data1.Level,
                        SaleDate1 = time1,
                        SaleAmount1 = data1.SaleAmount,
                        SaleDate2 = time2,
                        SaleAmount2 = data2.SaleAmount,
                        SaleDate3 = time3,
                        SaleAmount3 = data3.SaleAmount,
                    };
                    if (item.Key.ShopID == "KM9999999999999999999")
                    {
                        var calSaleAmount12 = query.Sum(d => d.CalSaleAmount12);
                        var calSaleAmount13 = query.Sum(d => d.CalSaleAmount13);
                        var calSaleAmount2 = query.Sum(d => d.CalSaleAmount2);
                        var calSaleAmount3 = query.Sum(d => d.CalSaleAmount3);
                        newItem.Compared2 = calSaleAmount2 == 0
                            ? 0
                            : ((calSaleAmount12 / calSaleAmount2) - 1) * 100;
                        newItem.Compared3 = calSaleAmount3 == 0
                            ? 0
                            : ((calSaleAmount13 / calSaleAmount3) - 1) * 100;
                    }
                    else
                    {
                        newItem.Compared2 = newItem.SaleAmount2 == 0
                            ? 0
                            : ((newItem.SaleAmount1 / newItem.SaleAmount2) - 1) * 100;
                        newItem.Compared3 = newItem.SaleAmount3 == 0
                            ? 0
                            : ((newItem.SaleAmount1 / newItem.SaleAmount3) - 1) * 100;
                    }
                    newList.Add(newItem);
                }
                return newList;
            }
            catch (Exception ex)
            {
                throw(ex);
            }
        }

        public IList<KMSalesYearOnYearReportModel.ShowModel> GetReport(DateTime EndDate1,
            DateTime EndDate2, DateTime EndDate3, int Level)
        {
            try
            {
                if (Level == 0)
                {
                    return GetOperatorAreaReport(EndDate1, EndDate2, EndDate3).ToList();
                }
                else if (Level == 1)
                {
                    return GetShopReport(EndDate1, EndDate2, EndDate3).ToList();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return null;
            }
        }
    }
}
